package com.yidi.common.core.domain.entity;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import com.yidi.common.utils.StringUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import com.yidi.common.annotation.Excel;
import com.yidi.common.annotation.Excel.Type;
import com.yidi.common.annotation.Excels;
import com.yidi.common.core.domain.BaseEntity;

/**
 * @author ：Xn
 * @ClassName: SysUser
 * @date ：2021-10-18 14:15
 * @describe ：用户对象
 */
@Getter
@Setter
@Entity
@Table(name = "sys_user")
@ToString(callSuper=true)
public class SysUser extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 部门ID */
    @Excel(name = "部门编号", type = Type.IMPORT)
    @Column(name = "dept_id")
    private Long deptId;

    /** 用户账号 */
    @Excel(name = "登录名称")
    @NotBlank(message = "用户账号不能为空")
    @Size(min = 0, max = 30, message = "登录账号长度不能超过30个字符")
    @Column(name = "user_name")
    private String userName;

    /** 用户昵称 */
    @Excel(name = "用户名称")
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    @Column(name = "nick_name")
    private String nickName;

    /** 用户邮箱 */
    @Excel(name = "用户邮箱")
    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    private String email;

    /** 手机号码 */
    @Excel(name = "手机号码")
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    private String phonenumber;

    /** 用户性别 */
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    private String sex;

    /** 用户头像 */
    private String avatar;

    /** 密码 */
    private String password;

    /** 盐加密 */
    @Transient //不在数据库中做映射
    private String salt;

    /** 帐号状态（0正常 1停用） */
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    private String status;

    /** 最后登录IP */
    @Excel(name = "最后登录IP", type = Type.EXPORT)
    @Column(name = "login_ip")
    private String loginIp;

    /** 最后登录时间 */
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
    @Column(name = "login_date")
    private Date loginDate;

    /** 部门对象 */
    @Excels({
        @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
        @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
    })
    @Transient //不在数据库中做映射
    private SysDept dept;

    /** 角色对象 */
    @Transient //不在数据库中做映射
    private List<SysRole> roles;

    /** 角色组 */
    @Transient //不在数据库中做映射
    private Long[] roleIds;

    /** 岗位组 */
    @Transient //不在数据库中做映射
    private Long[] postIds;

    /** 角色ID */
    @Transient //不在数据库中做映射
    private Long roleId;

    public SysUser()
    {

    }

    public SysUser(Long userId)
    {
        this.setId(userId);
    }

    public boolean isAdmin()
    {
        return isAdmin(this.userName);
    }

    public static boolean isAdmin(String userName)
    {
        return StringUtils.isNotBlank(userName) && "admin".equals(userName);
    }

}
